//
//  BTStickerKeyboardView.m
//  BaiTengIM
//
//  Created by awh on 2019/9/16.
//  Copyright © 2019 awh. All rights reserved.
//

#import "BTStickerKeyboardView.h"
#import "HLHorizontalPageLayout.h"
#import "StickerModel.h"

#import "StickerCell.h"

@interface BTStickerKeyboardView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet HLHorizontalPageLayout *layout;
@property (weak, nonatomic) IBOutlet UIPageControl *pageC;
@property (weak, nonatomic) IBOutlet UIButton *senderBtn;

@property(nonatomic, strong) NSArray *dataSources;

@end

@implementation BTStickerKeyboardView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.senderBtn.layer.masksToBounds = YES;
    self.senderBtn.layer.cornerRadius = 5.0f;
    
    self.pageC.pageIndicatorTintColor = [UIColor grayColor];
    self.pageC.currentPageIndicatorTintColor = KHEXCOLOR(kMainColor);
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    _layout.sectionInset = UIEdgeInsetsMake(10,10, 10, 10);
    _layout.minimumInteritemSpacing = 10;
    _layout.minimumLineSpacing = 10;
    _layout.itemSize = CGSizeMake(30, 30);
    
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[StickerCell class] forCellWithReuseIdentifier:[StickerCell cellIdentifier]];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"BTEmojiList"ofType:@"plist"];
    
    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *stickers = dictionary[@"emojiArray"];
    stickers = [StickerModel mj_objectArrayWithKeyValuesArray:stickers];
//    NSData *data=[NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
//    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    self.dataSources = [NSArray arrayWithArray:stickers];
    [self.collectionView reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.pageC.numberOfPages = ceil(self.collectionView.contentSize.width / SCREEN_WIDTH);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSources.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StickerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[StickerCell cellIdentifier] forIndexPath:indexPath];
    cell.sticker = self.dataSources[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    StickerModel *sticker = self.dataSources[indexPath.item];
    if (self.delegate && [self.delegate respondsToSelector:@selector(stickerKeyboard:didClickAtSticker:)]) {
        [self.delegate stickerKeyboard:self didClickAtSticker:sticker];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / SCREEN_WIDTH ;
    self.pageC.currentPage = page;
}


- (IBAction)deleteAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(stickerKeyboardDidClickDeleteButton:)]) {
        [self.delegate stickerKeyboardDidClickDeleteButton:self];
    }
}
- (IBAction)senderAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(stickerKeyboardDidClickSendButton:)]) {
        [self.delegate stickerKeyboardDidClickSendButton:self];
    }
}

@end
